
package com.example.origin.spider.demo;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


public class TradingeConomicsSeleniumDemo {
    public static void main(String[] args) {

//        ChromeOptions options = new ChromeOptions();
//        WebDriver driver = new ChromeDriver(options);
//        driver.get("https://www.selenium.dev/selenium/web/web-form.html");


        ChromeDriver driver = null;
        System.getProperties().setProperty("webdriver.chrome.driver", "D:\\workspaces\\idea_workspaces\\origin_workspace\\origin\\origin-spider\\src\\main\\resources\\chromedriver.exe");
//        if (!ObjectUtils.isEmpty(property)) {
        List<String> list = new ArrayList<>();
        list.add("headless");
        list.add("disable-gpu");
        ChromeOptions options = new ChromeOptions();
        options.addArguments(list);
        options.addArguments("--remote-allow-origins=*");
        options.addArguments("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0");
        driver = new ChromeDriver(options);


        try {
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

            driver.get("https://zh.tradingeconomics.com/china/consumer-price-index-cpi");
            Thread.sleep(5 * 1000);
            //获取当前页面的url
            String url = driver.getCurrentUrl();
            System.out.println(url);
            //获取当前页面的title
            String title = driver.getTitle();

            System.out.println(title);


//            String pageSource = driver.getPageSource();

//            System.out.println(pageSource);

            WebElement titleEle = driver.findElement(By.cssSelector(".title-indicator"));

            List<WebElement> elements = driver.findElements(By.cssSelector(".highcharts-point"));
            for (WebElement element : elements) {

                // 创建Actions对象
                Actions builder = new Actions(driver);

                // 使用moveToElement方法模拟鼠标悬停
                builder.moveToElement(element).perform();


                Thread.sleep(2000);
                WebElement el = driver.findElement(By.cssSelector(".tooltip-value"));
                String text = el.getText();
                el = driver.findElement(By.cssSelector(".tooltip-date"));
                String text2 = el.getText();
                System.out.println(text2+"  "+text);

                builder.moveToElement(titleEle).perform();

            }


        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            driver.quit();
        }

    }
}
